package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.ProductAttribute;
import com.zrrd.yunchmall.product.entity.ProductAttributeCategory;
import com.zrrd.yunchmall.product.service.IProductAttributeCategoryService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/productAttribute/category")
@Api(tags = "商品模块-类型")
public class ProductAttributeCategoryController {

    @Autowired
    private IProductAttributeCategoryService productAttributeCategoryService;

    @ApiOperation("查询商品类型")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam int pageSize,
                               @RequestParam int pageNum, String keyword) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.like("name", keyword);
        }
        IPage<ProductAttribute> pageInfo = productAttributeCategoryService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return new ResponseResult<>(200, "查询成功!", pageInfo);
    }
    @ApiOperation("删除商品类型")
    @GetMapping("/delete/{id}")
    public ResponseResult deleteById(@PathVariable("id") int id) {
        return new ResponseResult<>(200, "修改成功!", productAttributeCategoryService.removeById(id));
    }
    @ApiOperation("添加类型")
    @PostMapping ("/create")
    public ResponseResult create( String name){
        ProductAttributeCategory productAttributeCategory=new ProductAttributeCategory();
        if (!StringUtils.isEmpty(name)){
            return new ResponseResult<>(205,"商品类型已经存在");
        }
        productAttributeCategory.setName(name);
        return new ResponseResult<>(200,"添加成功!",productAttributeCategoryService.save(productAttributeCategory));
    }
    @ApiOperation("批量删除商品属性")
    @PostMapping("/delete")
    public ResponseResult deleteByIds(String ids) {
        for (String id:ids.split(",")) {
            productAttributeCategoryService.removeById(Long.valueOf(id));
        }
        return new ResponseResult<>(200, "修改成功!");
    }
    @ApiOperation("修改商品类型的编辑")
    @GetMapping("/list/withAttr")
    public ResponseResult withAttr() {
        return new ResponseResult<>(200, "查询成功!");
    }
    @ApiOperation("修改商品类型")
    @PostMapping("/update/{id}")
    public ResponseResult updateById(@PathVariable("id") int id,ProductAttributeCategory productAttributeCategory) {
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        return new ResponseResult<>(200, "查询成功!",productAttributeCategoryService.update(productAttributeCategory,updateWrapper));
    }
}
